package com.xiaochong.bitong.job.schedule;

import com.alibaba.fastjson.JSON;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.bitong.job.config.ExecutorConfig;
import com.xiaochong.bitong.job.config.hbase.HbaseBaseQueryComponent;
import com.xiaochong.bitong.job.service.MarketIndecService;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.vo.*;
import com.xiaochong.tir.common.exception.hbase.HbaseCloseExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseConnectionExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseGetTableExcetion;
import com.xiaochong.tir.common.util.RedisKeyApp;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Project : xc-tir
 * @Package Name : com.xiaochong.bitong.job.schedule
 * @Description : TODO
 * @Author : daizhiqing@xiaochong.com
 * @Creation Date : 2018年08月13日下午7:44
 * @ModificationHistory Who When What
 */
@Component
public class MarketIndexScheduled {

    @Autowired
    RedisServer redisServer;

    @Autowired
    MarketIndecService marketIndecService;

    @Autowired
    HbaseBaseQueryComponent hbaseBaseQueryComponent;

    /**
     *  3小时大盘数据图表 5分钟
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="30 0/5 * * * ?")
    public void broaderChart3Hours(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("broaderChart3Hours" , "broaderChart3Hours开始" , System.currentTimeMillis()+"");
            ChartVo chartVo = marketIndecService.getBroaderIndexOfThreeHours();

            if(chartVo != null){
                redisServer.set(RedisKeyApp.APP_TIR_BROADER_INDEX_CHART_3H , chartVo);
            }
            LogTrace.info("broaderChart3Hours" , "broaderChart3Hours结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }

    /**
     *  24小时大盘数据图表 10分钟
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="30 0/10 * * * ?")
    public void broaderChart24Hours(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("broaderChart24Hours" , "broaderChart24Hours开始" , System.currentTimeMillis()+"");
            ChartVo chartVo = marketIndecService.getBroaderIndexOf24Hours();

            if(chartVo != null){
                redisServer.set(RedisKeyApp.APP_TIR_BROADER_INDEX_CHART_24H , chartVo);
            }
            LogTrace.info("broaderChart24Hours" , "broaderChart24Hours结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }

    /**
     *  7天大盘数据图表 一小时
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 1 0/1 * * ?")
    public void broaderChart7Day(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("broaderChart7Day" , "broaderChart7Day开始" , System.currentTimeMillis()+"");
            ChartVo chartVo = marketIndecService.getBroaderIndexOf7Day();

            if(chartVo != null){
                redisServer.set(RedisKeyApp.APP_TIR_BROADER_INDEX_CHART_7D , chartVo);
            }
            LogTrace.info("broaderChart7Day" , "broaderChart7Day结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }

    /**
     *  30天大盘数据图表 一小时
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 1 0/4 * * ?")
    public void broaderChart30Day(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("broaderChart30Day" , "broaderChart30Day开始" , System.currentTimeMillis()+"");
            ChartVo chartVo = marketIndecService.getBroaderIndexOf30Day();

            if(chartVo != null){
                redisServer.set(RedisKeyApp.APP_TIR_BROADER_INDEX_CHART_30D , chartVo);
            }
            LogTrace.info("broaderChart30Day" , "broaderChart30Day结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }

    /**
     *  全部(一年)大盘数据图表 一天
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 30 0 * * ?")
    public void broaderChartYear(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("broaderChartYear" , "broaderChartYear开始" , System.currentTimeMillis()+"");
            ChartVo chartVo = marketIndecService.getBroaderIndexOfYear();

            if(chartVo != null){
                redisServer.set(RedisKeyApp.APP_TIR_BROADER_INDEX_CHART_1Y , chartVo);
            }
            LogTrace.info("broaderChart30Day" , "broaderChart30Day结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }


    /**
     * 赚钱效应组装数据放入redis中
     * @throws Exception
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="10 0/5 * * * ?")
    public void markMoneyEffectToCacheCalculation() throws Exception{
        LogTrace.beginTrace();
        try {
            LogTrace.info("赚钱效应组装数据放入redis中" , "markMoneyEffectToCacheCalculation" , System.currentTimeMillis()+"");
            GraliIntensity graliIntensity =  marketIndecService.MarkMoneyEffectToCacheCalculation();
            //数据插入到redis中
            redisServer.set(RedisKeysUtils.MARK_MONEY_EFFECT_FIVE_DATA,JSON.toJSONString(graliIntensity));
            LogTrace.info("赚钱效应组装结束" , "markMoneyEffectToCacheCalculation" , System.currentTimeMillis()+"");

        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error(e.getMessage() ,e);
        }finally {
            LogTrace.endTrace();
        }
    }


    /**
     * 5分钟组装量能变化redis中，
     * @throws Exception
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="20 0/5 * * * ?")
    public void marketInterpretationChangeToCacheCalculation(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("量能变化5分钟组装数据到redis中" , "marketInterpretationChangeToCacheCalculation" , System.currentTimeMillis()+"");
            ApiMarketInterpretationChange apiMarketInterpretationChange =  marketIndecService.MarketInterpretationChangeToCacheCalculation();
            redisServer.set(RedisKeysUtils.MARKET_INTERPRETATION_CHANGE,JSON.toJSONString(apiMarketInterpretationChange));
            LogTrace.info("量能变化5分钟组装结束" , "MarketInterpretationChangeToCacheCalculation" , System.currentTimeMillis()+"");
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }

    }

    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="25 0/5 * * * ?")
    public void marketWeightsPerformanceToCacheCalculation(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("权重表现5分钟组装数据到redis中" , "marketWeightsPerformanceToCacheCalculation" , System.currentTimeMillis()+"");
            ApiMarketWeightsPerformance apiMarketWeightsPerformance =  marketIndecService.MarketWeightsPerformanceToCacheCalculation();
            redisServer.set(RedisKeysUtils.MARKET_WEIGHTS_PERFORMANCE,JSON.toJSONString(apiMarketWeightsPerformance));
            LogTrace.info("权重表现5分钟组装结束" , "marketWeightsPerformanceToCacheCalculation" , System.currentTimeMillis()+"");
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }
}
